//===============================================================================
// Q4Tech Engineering Team
// Mobile Updater Application Block - November 2006
//===============================================================================
// Copyright  Q4Tech Informatica Argentina.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Mobile.Updater;
using Microsoft.ApplicationBlocks.Mobile.Updater.Xsd;
using Microsoft.WindowsMobile.Status;
using System.Xml;

namespace Microsoft.ApplicationBlocks.Mobile.Updater.Evaluators
{
    public class BatteryLevel : IConditionEvaluator
    {
        private double batteryLevel;
        private bool overrideLevelOnAcPower;

        #region IConditionEvaluator Members

        public void Init(ConditionEvaluatorProviderData data, Microsoft.ApplicationBlocks.Mobile.Updater.Manifest manifest)
        {
            foreach (XmlAttribute atrib in data.AnyAttr)
            {
                if (atrib.Name == "minLevel")
                    this.batteryLevel = double.Parse(atrib.Value);

                if (atrib.Name == "overrideLevelOnAcPower")
                    this.overrideLevelOnAcPower = bool.Parse(atrib.Value);
            }
        }

        public bool Evaluate()
        {
            if (overrideLevelOnAcPower &&
                SystemState.PowerBatteryState == BatteryState.Charging)
                    return true;

            double chargePercent = 21;
            
            switch (SystemState.PowerBatteryStrength)
            {
                case Microsoft.WindowsMobile.Status.BatteryLevel.VeryHigh:
                    chargePercent = 81;
                    break;
                case Microsoft.WindowsMobile.Status.BatteryLevel.High:
                    chargePercent = 61;
                    break;
                case Microsoft.WindowsMobile.Status.BatteryLevel.Medium:
                    chargePercent = 41;
                    break;
                case Microsoft.WindowsMobile.Status.BatteryLevel.Low:
                    chargePercent = 21;
                    break;
                case Microsoft.WindowsMobile.Status.BatteryLevel.VeryLow:
                    chargePercent = 0;
                    break;
            }

            return batteryLevel <= chargePercent;
        }

        public String RejectReason
        {
            get
            {
                return "Not enough battery power.";
            }
        }

        #endregion
    }
}
